package com.hoperun.checkcloth.adapter;

import android.content.Context;
import android.content.res.Resources;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;

import androidx.core.content.res.ResourcesCompat;

import com.hoperun.checkcloth.R;
import com.hoperun.checkcloth.entity.XiaciInfo;

import java.util.List;

public class StuffSJZDAdapter extends BaseAdapter {

    final private Context mContext;
    final private Resources mResources;
    private onBtnClickListener mOnBtnClickListener;
    final private List<XiaciInfo> mList;
    private int currentPosition = -1;

    public StuffSJZDAdapter(Context context, List<XiaciInfo> list) {
        super();
        mContext = context;
        mResources = mContext.getResources();
        mList = list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) { // 获得当前项的示例
        XiaciInfo xclx;
        xclx = (XiaciInfo) getItem(position);
        View view;
        final StuffSJZDAdapter.ViewHolder viewHolder;
        if (convertView == null) {
            view = LayoutInflater.from(mContext).inflate(R.layout.item_xclx_layout, parent, false);
            viewHolder = new StuffSJZDAdapter.ViewHolder(view);
            view.setTag(viewHolder);
        } else {
            view = convertView;
            viewHolder = (StuffSJZDAdapter.ViewHolder) view.getTag();
        }

        if (TextUtils.isEmpty(xclx.getSJXMC())) {
            viewHolder.btn.setVisibility(View.INVISIBLE);
            viewHolder.btn.setEnabled(false);
        } else {
            viewHolder.btn.setVisibility(View.VISIBLE);
            viewHolder.btn.setText(xclx.getSJXMC());
        }


        if (currentPosition != position) {
            viewHolder.btn.setBackground(ResourcesCompat.getDrawable(mResources, R.drawable.btn_corner_gray, null));
            viewHolder.btn.setTextColor(mResources.getColor(R.color.black));
        }

        viewHolder.btn.setOnClickListener(v -> {
            currentPosition = position;
            viewHolder.btn.setBackground(ResourcesCompat.getDrawable(mResources, R.drawable.btn_corner_red, null));
            viewHolder.btn.setTextColor(mResources.getColor(R.color.white));
            notifyDataSetChanged();
            if (mOnBtnClickListener != null) {
                mOnBtnClickListener.onClick(position);
            }
        });

        return view;
    }

    public void setOnBtnClickListener(onBtnClickListener onBtnClickListener) {
        mOnBtnClickListener = onBtnClickListener;
    }

    public interface onBtnClickListener {
        void onClick(int position);
    }


    static class ViewHolder {
        Button btn;

        public ViewHolder(View view) {
            btn = view.findViewById(R.id.btn);
        }
    }

}
